package MyDBCP;

import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MyDBCP {
    private static final BasicDataSource basicDataSource = new BasicDataSource();
    static {
        basicDataSource.setUrl(DB.URL);
        basicDataSource.setUsername(DB.USERNAME);
        basicDataSource.setPassword(DB.PASSWORD);
        basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
    }
    public static Connection getConnection(){
        try {
            return basicDataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void close(ResultSet rs, PreparedStatement ps,Connection con){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (con != null){
            try {
                con.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
